package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.qiniu.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;


    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        log.debug("SetmealController： getSetmeal");

        Jedis jedis = jedisPool.getResource();

        String setmeal = jedis.get("Asetmeal");

        List<Setmeal> setmealList=null;

        if(setmeal==null){
             setmealList = setmealService.findAll();

            String SetMealListStr = JSON.toJSONString(setmealList);

            jedis.set("Asetmeal",SetMealListStr);
        }else {

             setmealList = JSONObject.parseArray(setmeal, Setmeal.class);
        }

        return new Result(true,MessageConst.QUERY_SETMEALLIST_SUCCESS,setmealList);

    }
    @RequestMapping("/findDetailsById")
    public Result findDetailsById(Integer id){
        log.debug("SetmealController:findDetailById: " + id);

        Jedis jedis = jedisPool.getResource();

        String setmealDatailAndIdstr =jedis.get("Bsetmeal" + id);

        Setmeal setmealdetailById=null;

        if(setmealDatailAndIdstr==null){

            setmealdetailById = setmealService.findDetailById(id);

            String jsonString = JSON.toJSONString(setmealdetailById);

            jedis.set("Bsetmeal" + id,jsonString);

        }else{

             setmealdetailById = JSONObject.parseObject(setmealDatailAndIdstr, Setmeal.class);

        }

        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmealdetailById);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("SetmealController:findById: " + id);

        Jedis jedis = jedisPool.getResource();

        String setmealToId = jedis.get("CsetmealToId"+id);

        Setmeal setmealById=null;

        if(setmealToId==null){

            setmealById = setmealService.findById(id);

            String jsonString = JSON.toJSONString(setmealById);

            jedis.set("CsetmealToId"+id,jsonString);

        }else {

            setmealById=JSONObject.parseObject(setmealToId,Setmeal.class);

        }

        log.debug("根据id查询套餐成功");

        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmealById);
    }



}
